<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :keyboard="false"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="联系人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入联系人姓名" v-decorator="['contactName', validatorRules.contactName]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item label="联系人职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入联系人职位" v-decorator="['position']" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item label="联系人电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入联系人电话" v-decorator="['phone']" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item label="联系人手机" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入联系人手机" v-decorator="['mobile', validatorRules.mobile]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item label="联系人邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入联系人邮箱" v-decorator="['email', validatorRules.email]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item label="是否主要联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch v-decorator="['isPrimary', {valuePropName: 'checked'}]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea placeholder="请输入备注" v-decorator="['remark']" :rows="4" :disabled="disableSubmit"/>
        </a-form-item>
      </a-form>
    </a-spin>
    
  </j-modal>
</template>

<script>
  import { contactAdd, contactEdit } from '@/api/crm'
  import pick from 'lodash.pick'
  import { validateEmail, validateMobile } from '@/utils/validate'

  export default {
    name: "ContactModal",
    data() {
      return {
        title: "操作",
        width: 600,
        visible: false,
        model: {},
        labelCol: { span: 5 },
        wrapperCol: { span: 16 },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          contactName: { 
            rules: [{ required: true, message: '请输入联系人姓名!' }] 
          },
          mobile: { 
            rules: [{ validator: this.validateMobile }] 
          },
          email: { 
            rules: [{ validator: this.validateEmail }] 
          }
        },
        disableSubmit: false,
        customerId: ''
      }
    },
    methods: {
      add() {
        this.edit({isPrimary: false});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 
            'contactName', 'position', 'phone', 'mobile', 'email', 'isPrimary', 'remark'))
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        if (this.disableSubmit) {
          this.close();
          return;
        }
        
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            formData.customerId = this.customerId;
            
            let obj;
            if (!this.model.id) {
              obj = contactAdd(formData);
            } else {
              obj = contactEdit(formData);
            }
            
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            });
          }
        });
      },
      handleCancel() {
        this.close();
      },
      validateMobile(rule, value, callback) {
        if (value && !validateMobile(value)) {
          callback('请输入正确的手机号码!');
        } else {
          callback();
        }
      },
      validateEmail(rule, value, callback) {
        if (value && !validateEmail(value)) {
          callback('请输入正确的电子邮箱!');
        } else {
          callback();
        }
      }
    }
  }
</script>

<style scoped>
</style> 